package com.jinke.tank;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.util.Objects;

public class MyTank extends Tank{

    private static final Image[] tankImg;
    static {
        tankImg = new Image[4];
        try {
            tankImg[0] = ImageIO.read(Objects.requireNonNull(com.jinke.tank.MyTank.class.getResourceAsStream("/img/u.png")));
            tankImg[1] = ImageIO.read(Objects.requireNonNull(com.jinke.tank.MyTank.class.getResourceAsStream("/img/d.png")));
            tankImg[2] = ImageIO.read(Objects.requireNonNull(com.jinke.tank.MyTank.class.getResourceAsStream("/img/l.png")));
            tankImg[3] = ImageIO.read(Objects.requireNonNull(com.jinke.tank.MyTank.class.getResourceAsStream("/img/r.png")));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public MyTank(int x, int y, int dir, int boundWidth, int boundHeight) {
        super(x, y, dir, boundWidth, boundHeight);
    }

    public MyTank(int x, int y, int dir) {
        super(x, y, dir);
    }

    @Override
    public void logic() {
        switch (state){
            case STATE_NORMAL:
                break;
            case STATE_MOVE:
                move();
                break;
            case STATE_DEAD:
                die(this);
                break;
        }
    }

    /**
     * 更新移动状态
     */
    public void updateMoveState() {
        // 按优先级检查按键状态：上>下>左>右（可根据需要调整优先级）
        if (keyStates[0]) { // 上方向被按下
            setDir(DIR_UP);
            setState(STATE_MOVE);
        } else if (keyStates[1]) { // 下方向被按下
            setDir(DIR_DOWN);
            setState(STATE_MOVE);
        } else if (keyStates[2]) { // 左方向被按下
            setDir(DIR_LEFT);
            setState(STATE_MOVE);
        } else if (keyStates[3]) { // 右方向被按下
            setDir(DIR_RIGHT);
            setState(STATE_MOVE);
        } else { // 没有方向键被按下
            setState(STATE_NORMAL);
        }
    }

    @Override
    public void drawImgTank(Graphics g) {
        g.drawImage(tankImg[dir], x-RADIUS, y-RADIUS, null);
    }
}
